package dao;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for
 * RoleTab entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see dao.RoleTab
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class RoleTabDAO {
	private static final Logger log = LoggerFactory.getLogger(RoleTabDAO.class);
	// property constants
	public static final String USER_ID = "userId";
	public static final String ROLE_NAME = "roleName";
	public static final String ICON = "icon";
	public static final String LEV = "lev";
	public static final String DIAMON = "diamon";
	public static final String IS_ONLINE = "isOnline";
	public static final String SUM_LOGIN_COUNT = "sumLoginCount";
	public static final String SERVER_ID = "serverId";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(RoleTab transientInstance) {
		log.debug("saving RoleTab instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(RoleTab persistentInstance) {
		log.debug("deleting RoleTab instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public RoleTab findById(java.lang.Integer id) {
		log.debug("getting RoleTab instance with id: " + id);
		try {
			RoleTab instance = (RoleTab) getCurrentSession().get("dao.RoleTab",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<RoleTab> findByExample(RoleTab instance) {
		log.debug("finding RoleTab instance by example");
		try {
			List<RoleTab> results = (List<RoleTab>) getCurrentSession()
					.createCriteria("dao.RoleTab").add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding RoleTab instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from RoleTab as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<RoleTab> findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List<RoleTab> findByRoleName(Object roleName) {
		return findByProperty(ROLE_NAME, roleName);
	}

	public List<RoleTab> findByIcon(Object icon) {
		return findByProperty(ICON, icon);
	}

	public List<RoleTab> findByLev(Object lev) {
		return findByProperty(LEV, lev);
	}

	public List<RoleTab> findByDiamon(Object diamon) {
		return findByProperty(DIAMON, diamon);
	}

	public List<RoleTab> findByIsOnline(Object isOnline) {
		return findByProperty(IS_ONLINE, isOnline);
	}

	public List<RoleTab> findBySumLoginCount(Object sumLoginCount) {
		return findByProperty(SUM_LOGIN_COUNT, sumLoginCount);
	}

	public List<RoleTab> findByServerId(Object serverId) {
		return findByProperty(SERVER_ID, serverId);
	}

	public List findAll() {
		log.debug("finding all RoleTab instances");
		try {
			String queryString = "from RoleTab";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public RoleTab merge(RoleTab detachedInstance) {
		log.debug("merging RoleTab instance");
		try {
			RoleTab result = (RoleTab) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(RoleTab instance) {
		log.debug("attaching dirty RoleTab instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(RoleTab instance) {
		log.debug("attaching clean RoleTab instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static RoleTabDAO getFromApplicationContext(ApplicationContext ctx) {
		return (RoleTabDAO) ctx.getBean("RoleTabDAO");
	}
}